package com.example.flowermall.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.flowermall.entity.*;
import com.example.flowermall.service.IProductImgService;
import com.example.flowermall.service.IntegralProductService;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("integralProduct")
public class IntegralProductController {
    private final IntegralProductService integralProductService;
    private final IProductImgService productImgService;

    public IntegralProductController(IntegralProductService integralProductService, IProductImgService productImgService) {
        this.integralProductService = integralProductService;
        this.productImgService = productImgService;
    }

    @GetMapping("/list")
    public Result getList(@RequestParam(value = "pageNo",required = false)Long pageNo,
                          @RequestParam(value = "pageSize",required = false)Long pageSize){
        Page<IntegralProduct> page=new Page<>(pageNo,pageSize);
        Map<String,Object> data=new HashMap<>();
        Result result=Result.success();
        LambdaQueryWrapper<IntegralProduct> queryWrapper=new LambdaQueryWrapper<>();
        Page<IntegralProduct> page1 = integralProductService.page(page, queryWrapper);
        List<IntegralProduct> records = page1.getRecords();
        for(int i=0;i<records.size();i++){
            List<ProductImg> listByProductId = productImgService.getListByIntegralProductId(records.get(i).getProductId());
            ProductImg byProductId = productImgService.getByIntegralProductId(records.get(i).getProductId());
            if(byProductId!=null){
                records.get(i).setProductUrl(byProductId.getImgUrl());
            }
            if(listByProductId!=null){
                records.get(i).setProductImgList(listByProductId);
            }
        }
        data.put("total",page.getTotal());
        data.put("rows",records);
        result.put("data",data);
        return result;
    }


    @PostMapping
    public Result insertGoods(@RequestBody IntegralProduct product){
        return Result.success(integralProductService.insert(product));
    }

    @PutMapping
    public Result updateGoods(@RequestBody IntegralProduct product){
        integralProductService.update(product);
        return Result.success("修改成功");
    }
    @DeleteMapping("/{id}")
    public Result deleteGoods(@PathVariable("id")Integer id){
        integralProductService.deleteById(id);
        return Result.success("删除成功");
    }
    @GetMapping("/{id}")
    public Result getOrderById(@PathVariable("id")Integer id){
        LambdaQueryWrapper<IntegralProduct> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(IntegralProduct::getProductId,id);
        IntegralProduct integralProduct = integralProductService.getOne(queryWrapper);
        List<ProductImg> list = productImgService.getListByIntegralProductId(id);
        integralProduct.setProductImgList(list);
        Result result=Result.success();
        result.put("data", integralProduct);
        return result;
    }
}
